package com.wxy;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManagementSystem {
    private static final ArrayList<Student> students = new ArrayList<>();
    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("----- 欢迎使用学生管理系统 -----");
            System.out.println("1. 添加学生");
            System.out.println("2. 删除学生");
            System.out.println("3. 修改学生");
            System.out.println("4. 查询学生");
            System.out.println("5. 退出系统");
            System.out.print("请输入您的选择：");

            String choice = scanner.next();
            switch (choice) {
                case "1" -> addStudent();
                case "2" -> deleteStudent();
                case "3" -> modifyStudent();
                case "4" -> showStudents();
                case "5" -> {
                    System.out.println("感谢您的使用，再见！");
                    System.exit(0);
                }
                default -> System.out.println("输入错误，请重新输入！");
            }
        }
    }

    // 添加学生
    private static void addStudent() {
        Student student = new Student();
        while (true) {
            System.out.print("请输入学号：");
            String id = scanner.next();
            if (getIndex(id) == -1) {
                student.setId(id);
                break;
            }
            System.out.println("你输入的学号已被占用，请重新输入");
        }

        System.out.print("请输入姓名：");
        student.setName(scanner.next());
        System.out.print("请输入年龄：");
        student.setAge(scanner.nextInt());
        System.out.print("请输入居住地：");
        student.setAddress(scanner.next());

        students.add(student);
        System.out.println("添加学生成功");
    }

    // 查看学生
    private static void showStudents() {
        if (students.isEmpty()) {
            System.out.println("无信息，请先添加信息再查询！");
            return;
        }

        System.out.println("学号\t姓名\t年龄\t地址");
        for (Student student : students) {
            System.out.printf(
                    "%s\t%s\t%d岁\t%s%n",
                    student.getId(),
                    student.getName(),
                    student.getAge(),
                    student.getAddress()
            );
        }
    }

    // 删除学生
    private static void deleteStudent() {
        System.out.print("请输入要删除的学生学号：");
        String id = scanner.next();
        int index = getIndex(id);
        if (index == -1) {
            System.out.println("该信息不存在，请重新输入！");
        } else {
            students.remove(index);
            System.out.println("删除学生成功！");
        }
    }

    // 修改学生
    private static void modifyStudent() {
        System.out.print("请输入要修改的学生学号：");
        String id = scanner.next();
        int index = getIndex(id);
        if (index == -1) {
            System.out.println("该信息不存在，请重新输入！");
            return;
        }

        Student student = students.get(index);
        System.out.print("请输入学生新姓名（原值：" + student.getName() + "）：");
        student.setName(scanner.next());
        System.out.print("请输入学生新年龄（原值：" + student.getAge() + "）：");
        student.setAge(scanner.nextInt());
        System.out.print("请输入学生新居住地（原值：" + student.getAddress() + "）：");
        student.setAddress(scanner.next());
        System.out.println("修改学生成功");
    }

    // 检查学号是否存在并返回索引
    private static int getIndex(String id) {
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }
}
